package com.softeem.score.manager.controller.admin;

import com.github.pagehelper.PageInfo;
import com.softeem.score.common.log.annotation.Log;
import com.softeem.score.common.log.enums.OperatorType;
import com.softeem.score.manager.service.CourseService;
import com.softeem.score.model.dto.CourseDto;
import com.softeem.score.model.dto.GradeComponentDto;
import com.softeem.score.model.enmu.CourseBelongingEnum;
import com.softeem.score.model.enmu.CourseCategoryEnum;
import com.softeem.score.model.enmu.CourseNatureEnum;
import com.softeem.score.model.vo.common.Result;
import com.softeem.score.model.vo.common.ResultCodeEnum;
import com.softeem.score.model.vo.educational.CourseVo;
import com.softeem.score.model.vo.educational.GradeCompontWeightVo;
import com.softeem.score.model.vo.educational.SchedulesVo;
import com.softeem.score.model.vo.school.MapVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2024-02-14
 */
@Tag(name = "课程管理")
@RestController
@RequestMapping("/admin/educational/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Operation(summary = "分页查询课程列表")
    @GetMapping(value = "/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<CourseVo>> findByPage(CourseDto courseDto,
                                                 @PathVariable(value = "pageNum") Integer pageNum,
                                                 @PathVariable(value = "pageSize") Integer pageSize) {
        PageInfo<CourseVo> pageInfo = courseService.findByPage(courseDto, pageNum, pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "通过课程id查询课程信息")
    @GetMapping(value = "/getCourseById")
    public Result getCourseById(@RequestParam(name = "courseIdList", required = false) List<Long> courseIdList) {
        if (courseIdList != null) {
            List<MapVo> courseList = courseService.findCourseById(courseIdList);
            return Result.build(courseList, ResultCodeEnum.SUCCESS);
        } else {
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }
    }

    @Operation(summary = "查询学院所有的课程")
    @GetMapping(value = "/findCourseByCollegeId/{collegeId}")
    public Result findCourseByCollegeId(@PathVariable(value = "collegeId") Long collegeId) {
        List<MapVo> courseList = courseService.findCourseByCollegeId(collegeId);
        return Result.build(courseList, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "保存课程", businessType = 1, operatorType = OperatorType.MANAGE)
    @Operation(summary = "保存课程")
    @PostMapping(value = "/saveCourse")
    public Result saveCourse(@RequestBody CourseVo courseVo) {
        courseService.saveCourse(courseVo);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "更新课程", businessType = 2, operatorType = OperatorType.MANAGE)
    @Operation(summary = "更新课程")
    @PutMapping(value = "/updateCourse")
    public Result updateCourse(@RequestBody CourseVo courseVo) {
        courseService.updateCourse(courseVo);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "通过id删除课程", businessType = 3, operatorType = OperatorType.MANAGE)
    @Operation(summary = "通过id删除课程")
    @DeleteMapping(value = "/deleteById/{courseId}")
    public Result deleteById(@PathVariable(value = "courseId") Long courseId) {
        boolean flag = courseService.deleteById(courseId);
        return Result.build(flag, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询所有成绩项")
    @GetMapping(value = "/findAllGradeComponents")
    public Result findAllGradeComponents() {
        List<MapVo> gradeComponents = courseService.findAllGradeComponents();
        return Result.build(gradeComponents, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "通过课程id查询成绩项及权重")
    @GetMapping(value = "/selectGradeComponentByCourseId/{courseId}")
    public Result selectGradeComponentByCourseId(@PathVariable(value = "courseId") Long courseId) {
        List<GradeCompontWeightVo> gradeComponents = courseService.selectGradeComponentByCourseId(courseId);
        return Result.build(gradeComponents, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "设置课程成绩计算权重", businessType = 2, operatorType = OperatorType.TEACHER)
    @Operation(summary = "设置课程成绩计算权重")
    @PutMapping(value = "/updateGradeComponentsWeight")
    public Result updateGradeComponentsWeight(@RequestBody GradeComponentDto gradeComponentDto) {
        courseService.updateGradeComponentsWeight(gradeComponentDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "课程类别集合")
    @GetMapping(value = "/findAllCategory")
    public Result findAllCategory() {
        List<MapVo> categoryList = new ArrayList<>();
        for (CourseCategoryEnum categoryEnum : CourseCategoryEnum.values()) {
            MapVo mapVo = new MapVo();
            mapVo.setId((long) categoryEnum.getId());
            mapVo.setName(categoryEnum.getValue());
            categoryList.add(mapVo);
        }
        return Result.build(categoryList, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "课程性质集合")
    @GetMapping(value = "/findAllNature")
    public Result findAllNature() {
        List<MapVo> natureList = new ArrayList<>();
        for (CourseNatureEnum navCategoryEnum : CourseNatureEnum.values()) {
            MapVo mapVo = new MapVo();
            mapVo.setId((long) navCategoryEnum.getId());
            mapVo.setName(navCategoryEnum.getValue());
            natureList.add(mapVo);
        }
        return Result.build(natureList, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "课程归属集合")
    @GetMapping(value = "/findAllOwnership")
    public Result findAllOwnership() {
        List<MapVo> owernshipList = new ArrayList<>();
        for (CourseBelongingEnum courseBelongingEnum : CourseBelongingEnum.values()) {
            MapVo mapVo = new MapVo();
            mapVo.setId((long) courseBelongingEnum.getId());
            mapVo.setName(courseBelongingEnum.getValue());
            owernshipList.add(mapVo);
        }
        return Result.build(owernshipList, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "筛选获取学院的课程", businessType = 0)
    @Operation(summary = "筛选获取学院的课程")
    @GetMapping(value = "/fetchLocalCoursesByCollegeId/{collegeId}")
    public Result fetchLocalCoursesByCollegeId(@PathVariable(value = "collegeId") Long collegeId,
                                               @RequestParam(value = "category", required = false) Integer category,
                                               @RequestParam(value = "nature", required = false) Integer nature) {
        List<MapVo> localCourseList = courseService.findByCollegeId(collegeId, category, nature);
        return Result.build(localCourseList, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "通过班级ID获取课程表信息", businessType = 0)
    @Operation(summary = "通过班级ID获取课程表信息")
    @GetMapping(value = "/GetScheduleListByMajorIdAndClassesId/{classesId}")
    public Result<List<SchedulesVo>> GetScheduleListByMajorIdAndClassesId(@PathVariable(value = "classesId") Long classesId) {
        List<SchedulesVo> allCourseList = courseService.findSchedulesByclassId(classesId);
        return Result.build(allCourseList, ResultCodeEnum.SUCCESS);
    }


    @Log(title = "通过学生学号获取课程表信息", businessType = 0)
    @Operation(summary = "通过学生学号获取课程表信息")
    @GetMapping(value = "/getScheduleListByStudentNumber/{studentNumber}")
    public Result<List<SchedulesVo>> getScheduleListByStudentNumber(@PathVariable(value = "studentNumber") String studentNumber) {
        List<SchedulesVo> allCourseList = courseService.findSchedulesByStudentNumber(studentNumber);
        return Result.build(allCourseList, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "获取课程教师", businessType = 0, operatorType = OperatorType.MANAGE)
    @Operation(summary = "获取课程教师")
    @GetMapping(value = "/GetAllTeacherByCourseId/{courseId}")
    public Result<List<MapVo>> GetAllTeacherByCourseId(@PathVariable(value = "courseId") Long courseId) {
        List<MapVo> voList = courseService.findTeacherByCourseId(courseId);
        return Result.build(voList, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "获取课程信息", businessType = 0, operatorType = OperatorType.MANAGE)
    @Operation(summary = "获取课程信息")
    @GetMapping(value = "/FindAllTeacherCoursesByTeacherCode/{teacherCode}")
    public Result<List<CourseVo>> FindAllTeacherCoursesByTeacherCode(@PathVariable(value = "teacherCode") String teacherCode) {
        List<CourseVo> courseVoList = courseService.FindAllTeacherCoursesByTeacherCode(teacherCode);
        return Result.build(courseVoList, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "获取所有课程", businessType = 0, operatorType = OperatorType.MANAGE)
    @Operation(summary = "获取所有课程")
    @GetMapping(value = "/findAllCourse")
    public Result<List<MapVo>> findAllCourse() {
        List<MapVo> courseList = courseService.findAllCourse();
        return Result.build(courseList, ResultCodeEnum.SUCCESS);
    }

}

